Imports System.Data
Imports System.Xml
Imports DotNetPM.Business.Questionnaire

Partial Class Modules_Questionnaires_QuestionCreate
    Inherits baseWebPage

#Region "Private Area"

    Private _uc As UserControl
    Private _question As Question = Nothing

    Private Sub LoadMyControl()
        If _uc Is Nothing Then
            Dim config As String = String.Empty
            Dim type As Integer
            If _question Is Nothing Then
                If ddlType.SelectedItem Is Nothing Then
                    type = 1
                Else
                    type = ddlType.SelectedValue
                End If
            Else
                config = _question.Configuration
                type = CInt(config.Substring(0, 1))
                ddlType.SelectedValue = type
                ddlType.Enabled = False

                ' Getting the choices
                If type = 4 OrElse type = 5 Then
                    Session("Choices") = QuestionODS.GetChoices(_question.ID)
                End If

                txtQuestion.Text = _question.Question
                ddlCategory.SelectedValue = _question.CategoryID
            End If
            Dim myXML As XmlDocument = ControlXDS.GetXmlDocument
            Dim myNode As XmlNode = myXML.SelectSingleNode("/controlList/control[@id=" & type & "]")
            _uc = LoadControl(ResolveUrl("~/Secure/Controls/cfg" & myNode.Attributes("control").Value))
            _uc.ID = "cfgUC"
            CType(_uc, DotNetPM.Business.Questionnaire.UserControls.IConfigUC).AnswerType = type
            ' Use try loop to avoid "Load ViewState Error"
            _uc.EnableViewState = False
            ph1.Controls.Add(_uc)
            If Not String.IsNullOrEmpty(config) Then
                CType(_uc, DotNetPM.Business.Questionnaire.UserControls.IConfigUC).Config = config
            End If
        End If
    End Sub

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        hlBack.NavigateUrl = MyBase.GetPreviousPage
        If Not IsPostBack Then
            Dim questionId As String = Request.QueryString("qid")
            If String.IsNullOrEmpty(questionId) Then
                ddlCategory.SelectedValue = Session("questionnaire_category")
            Else
                _question = QuestionODS.GetQuestion(CInt(questionId))
                hfQuestionId.Value = _question.ID
            End If
        End If
        LoadMyControl()
        txtQuestion.Focus()
        butUpdate.Visible = Not String.IsNullOrEmpty(hfQuestionId.Value)
    End Sub

    Protected Sub ddlType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlType.SelectedIndexChanged
        ' IndexChaged comes after Page_Load, so uc is always loaded.
        ' Not need to load uc again. Just call reset to clear the fields
        CType(_uc, DotNetPM.Business.Questionnaire.UserControls.IConfigUC).Reset()
    End Sub

    Protected Sub butCatEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butCatEdit.Click
        Response.Redirect("CategoryManager.aspx")
    End Sub

    Protected Function GetCategory(ByVal id As Integer) As String
        Return DotNetPm.Business.Questionnaire.CategoryODS.GetCategory(id)
    End Function


    Protected Sub butCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butCreate.Click
        Dim uc As DotNetPM.Business.Questionnaire.UserControls.IConfigUC = _
              CType(ph1.FindControl("cfgUC"), DotNetPM.Business.Questionnaire.UserControls.IConfigUC)

        'Response.Write(uc.Config & "<br/>")
        'TextBox1.Text = Server.HtmlEncode(txtQuestion.Text) ' &lt;image&gt;| %
        'TextBox2.Text = Server.HtmlDecode(txtQuestion.Text) ' <image>| %
        Dim q As Question
        If uc.AnswerType = 4 OrElse uc.AnswerType = 5 Then
            ' For MultipleChoices Types
            Dim dv As DataView = CType(Session("Choices"), DataTable).DefaultView
            dv.Sort = "SortID"
            q = QuestionODS.CreateQuestion(ddlCategory.SelectedValue, txtQuestion.Text, uc.Config, dv)
            'For Each dr As DataRowView In dv
            '    Response.Write("SID = " & dr("SID") & ", Choice = " & dr("Choice") & "<br/>")
            'Next
        Else
            q = QuestionODS.CreateQuestion(ddlCategory.SelectedValue, txtQuestion.Text, uc.Config)
        End If
        Dim prevPage As String = MyBase.GetPreviousPage
        If prevPage.Contains("?") Then
            Dim vals As String() = prevPage.Split("?")
            Dim pPage As String = vals(0).Substring(vals(0).LastIndexOf("/") + 1)
            If pPage = "QuestionManager.aspx" Then
                Response.Redirect(prevPage)
            End If
            Response.Redirect(vals(0) & "?questionId=" & q.ID & "&" & vals(1))
        End If
        Response.Redirect(prevPage)
    End Sub

    Protected Sub butUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butUpdate.Click
        Dim uc As DotNetPM.Business.Questionnaire.UserControls.IConfigUC = _
              CType(ph1.FindControl("cfgUC"), DotNetPM.Business.Questionnaire.UserControls.IConfigUC)

        If uc.AnswerType = 4 OrElse uc.AnswerType = 5 Then
            ' For MultipleChoices Types
            Dim dv As DataView = CType(Session("Choices"), DataTable).DefaultView
            If dv.Table.DataSet.HasChanges Then
                dv.Sort = "SortID"
                QuestionODS.UpdateQuestion(hfQuestionId.Value, ddlCategory.SelectedValue, txtQuestion.Text, uc.Config, dv)
            Else
                QuestionODS.UpdateQuestion(hfQuestionId.Value, ddlCategory.SelectedValue, txtQuestion.Text, uc.Config)
            End If
        Else
            QuestionODS.UpdateQuestion(hfQuestionId.Value, ddlCategory.SelectedValue, txtQuestion.Text, uc.Config)
        End If
        Response.Redirect(MyBase.GetPreviousPage)
    End Sub

    Protected Sub butCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butCancel.Click
        'Dim prevPage As String = MyBase.GetPreviousPage
        'prevPage = prevPage.Substring(0, prevPage.IndexOf("?"))
        'prevPage = prevPage.Substring(prevPage.LastIndexOf("/") + 1)
        'If prevPage = "QuestionManager.aspx" Then
        '    Response.Redirect(prevPage)
        'End If
        Response.Redirect(GetPreviousPage)
    End Sub

    Protected Sub ddlCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCategory.SelectedIndexChanged
        Session("questionnaire_category") = ddlCategory.SelectedValue
    End Sub
End Class
